package cn.t.util.casestudy.encode;


import cn.t.util.common.digital.HexUtil;
import org.mozilla.universalchardet.UniversalDetector;

import java.io.UnsupportedEncodingException;

public class JuniversalCharDetUsage {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String hexStr = "8000c2120003000163020100016103000d0001701200030001630800096d61696c2e73656e6400017204ffffffff000170120007000b636d644261736554696d6508000a31353634363334363931000a616c6c69616e63654964080000000974617267657455696408000f33343835303733323130303035333700057469746c650800000008636f6e74656e74730800053131313131000d73656e644c6f63616c54696d6508000a3135363436333436393000046e616d6508000ae596b55e5f5ee596b532";
        byte[] hexBytes = HexUtil.stringToBytes(hexStr);
        System.out.println(guessEncoding(hexBytes));
        System.out.println(new String(hexBytes, "WINDOWS-1252"));
    }

    public static String guessEncoding(byte[] bytes) {
        String DEFAULT_ENCODING = "UTF-8";
        UniversalDetector detector = new UniversalDetector(null);
        detector.handleData(bytes, 0, bytes.length);
        detector.dataEnd();
        String encoding = detector.getDetectedCharset();
        detector.reset();
        if (encoding == null) {
            encoding = DEFAULT_ENCODING;
        }
        return encoding;
    }
}
